package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        // 打印日志
        log.info("员工登录：{}", employeeLoginDTO);
        // 调用service层的方法
        Employee employee = employeeService.login(employeeLoginDTO);

        // 登录成功后，生成jwt令牌容器
        Map<String, Object> claims = new HashMap<>();
        // 将员工id数据添加到jwt令牌容器中
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        // 调用JwtUtil的方法生成令牌
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        // 封装员工登录成功的响应数据对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();
        // 响应给前端
        return Result.success(employeeLoginVO, MessageConstant.LOGIN_SUCCESS);
    }

    /**
     * 退出
     */
    @ApiOperation("员工退出登录")
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     */
    @ApiOperation("新增员工")
    @PostMapping
    public Result<?> add(@RequestBody EmployeeDTO employeeDTO) {
        // 打印日志
        log.info("新增员工：{}", employeeDTO);
        // 调用service层的方法
        employeeService.add(employeeDTO);
        // 响应前端
        return Result.success();
    }

    /**
     * 分页查询
     */
    @ApiOperation("员工列表分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        // 打印日志
        log.info("员工列表分页查询：{}", employeePageQueryDTO);
        // 调用service层的方法
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        // 响应前端
        return Result.success(pageResult);
    }

    /**
     * 更新员工启用状态
     */
    @ApiOperation("更新员工启用状态")
    @PostMapping("/status/{status}")
    public Result<?> updateEnableStatus(@PathVariable Integer status, Long id) {
        // 打印日志
        log.info("更新员工启用状态：{},{}", status, id);
        // 调用service层的方法
        employeeService.updateEnableStatus(status, id);
        // 响应前端
        return Result.success();
    }

    @ApiOperation("根据id获取详情")
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Integer id) {
        // 打印日志
        log.info("根据id获取详情：{}", id);
        // 调用service层的方法
        Employee employee = employeeService.getById(id);
        // 响应前端
        return Result.success(employee);
    }

    /**
     * 更新员工信息
     */
    @ApiOperation("更新员工信息")
    @PutMapping
    public Result<?> update(@RequestBody EmployeeDTO employeeDTO) {
        // 打印日志
        log.info("更新员工信息：{}", employeeDTO);
        // 调用service层的方法
        employeeService.update(employeeDTO);
        // 响应前端
        return Result.success();
    }
}
